imports('../index.less') ;
import search from '{com}search';
import treedata from './treedata';
import util from '{libs}/nui/core/util';
var ztree = require('{libs}/zTree/jquery.ztree.all');

return {
    init:function(){
        // var charts = echarts.init(document.getElementById('echarts'));
        // charts.setOption(chartsOption,true);
        // console.log(charts)
        // charts.showLoading();
        this.serviceTree($('[name=unit]'))
    },
    serviceTree(target){
        target.search({
            field:'name',
            fieldsid:'companyId',
            match:{
                field:'name',
                like:function(data, value){
                    return data.indexOf(value) !== -1 
                }
            },
            container:target.closest('.Tag'),
            empty:'无结果',
            nullable:true,
            focus:true,
            tabs:[{
                title:'组织架构',
                content:function(){
                    return `
                        <div id="ztree" class="ztree"></div>
                    `;
                },
                onShow(self){
                    var that = this;
                    if(!that.ztree){
                        var zTreeObj =treedata.companyList;
                        if(zTreeObj.length>0){
                            zTreeObj[0].open = true; //默认展开第一个节点
                        }
                        var setting = {
                            view: {
                                showIcon: false,
                                showLine:false,
                                selectedMulti:false,//是否允许同时选中多个节点，这个是默认值，
                                dblClickExpand:false//双击节点是否自动展开父节点
                            },
                            data: {
                                simpleData: {//是{true}否false使用简单数据模式
                                    enable: true
                                },
                                key: {
                                    title: "title"
                                }
                            },
                            callback:{
                                beforeClick:function(treeId, treeNode){
                                    that.toggle(treeNode);
                                    // return false
                                },
                                onClick: function(event, treeId, treeNode){
                                    // that.gradezTreeOnClick.call(that, event, treeId, treeNode ,elem );
                                    // alert(treeId)
                                    console.log(treeNode,'treeNode')
                                }
                            }
                        };
                        console.log(self)
                        that.ztree = $.fn.zTree.init(self.container.find('#ztree'), setting, zTreeObj);
                        self.queryData = that.data = that.ztree.transformToArray(that.ztree.getNodes());
                        that.toggleZtree()
                    }
                    else{
                        that.toggleZtree()
                    }
                }
            }],
            exist:true,
            
            tag:{
                multiple:false,
                focus:true,
                container:target.prev('div'),
                getData(self,elem){
                    return self._options.setValue(self,util.getData(elem.find('input')).result);
                }
            },
            toggleZtree:function(){
                var that = this, self = that.self;
                Nui.each(that.data, function(v){
                    if(that.selected(self, v)){
                        that.ztree.selectNode(v, true)
                    }
                    else{
                        that.ztree.cancelSelectedNode(v)
                    }
                })
            },
            toggle(treeNode){
                var that = this, self = that.self;
                var arr = [], all = [];
                var nodes = that.ztree.transformToArray(treeNode);
                // Nui.each(nodes, function(node){
                //     if((!node.children || !node.children.length) && node.id){
                //         var data = that.setValue(self, node);
                //         if(!that.selected(self, node)){
                //             arr.push(data)
                //         }
                //         all.push(data)
                //     }
                // })
                var data = that.setValue(self, nodes[0]);
                arr.push(data);
                all.push(data);
                //没有被选择的或者全部被选择的
                var err = target.closest('.ui-item').find('.ui-err')
                if(all.length === arr.length || !arr.length){
                    self.value(all);
                }
                //只有部分被选择
                else{
                    self.value(arr);
                }
                if(err.length){
                    err.html('');
                }
                self.value(data.text);
            },
            selected(self, data){
                var exist = false;
                Nui.each(self.tagData, function(val){
                    if(data.id == val.fields[self._options.fieldsid]){
                        exist = true;
                        return false;
                    }
                })
                return exist
            },
            setValue(self, data){
                var tmp = {
                    text:data.name,
                    fields:{
                        name:data.name
                    }
                }
                tmp.fields[self._options.fieldsid]= data.id;
                return tmp;
            },
            // onSelectBefore:function(self, data){
            //     console.log('onSelectBefore')
            //     if(data.children){
            //         this.toggle(data)
            //     }
            //     else{
            //         self.value(this.setValue(self, data))
            //     }
            //     self.hide();
            //     return false
            // },
            onSelect:function(self,data){//模糊搜索之后的点击事件
                self.value(data.name);
            },
            // onBlur:function(self, elem){
            //     console.log('onBlur')
            //     // self.value('');
            // },
            onChange(self){
                var that = this;
                self.activeTab.$container.find('.con-search-item').each(function(){
                    var elem = $(this), data = elem.data();
                    elem.toggleClass('s-crt', that.selected(self, data))
                })
                that.toggleZtree();
                self.hide();
            }
        })
    }
}